<?php

// +----------------------------------------------------------------------
// | Date: 2018-04-20
// +----------------------------------------------------------------------
// | Author: zj 
// +----------------------------------------------------------------------

namespace app\game\controller;

use think\Db;
use cmf\controller\AdminBaseController;

class AdminGameListController extends AdminBaseController {

    protected $plats = ['android' => 'Android', 'ios' => 'IOS', 'room' => '开房', 'joy' => '娱乐', 'npc' => 'NPC', 'rmb' => '1:1', 'ctrl' => '输赢控制'];

    public function index() {
        $key = $this->request->param('game_key');
        $where = array();
        if ($key) {
            $where['game_key'] = ['like', "%$key%"];
        }
        $data = Db::name('game_list')->where($where)->order('id DESC')->paginate(15);
        $data->appends(['game_key' => $key]);
        // 获取分页显示
        $page = $data->render();
        $this->assign('data', $data);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function add() {
        $platStr = $this->getPlat();
        $this->assign('platStr', $platStr);
        return $this->fetch();
    }

    //生成平台选项
    public function getPlat($arr = []) {
        $platStr = '';
        foreach ($this->plats as $k => $v) {
            if (in_array($k, $arr)) {
                $platStr .= ' <label class="checkbox-inline"><input type="checkbox" value="' . $k . '" checked name="plat[]"/>' . $v . '</label>';
            } else {
                $platStr .= ' <label class="checkbox-inline"><input type="checkbox" value="' . $k . '"  name="plat[]"/>' . $v . '</label>';
            }
        }
        return $platStr;
    }

    public function addPost() {
        $param = $this->request->param();
        $hasId = Db::name('game_list')->where(['game_id' => $param['game_id']])->find();
        if (!empty($hasId)) {
            $this->error('此游戏ID已经存在，请重新输入');
        }
        $hasKey = Db::name('game_list')->where(['game_key' => $param['game_key']])->find();
        if (!empty($hasKey)) {
            $this->error('此游戏KEY已经存在，请重新输入');
        }
        if (!empty($param['plat'])) {
            $plat = $param['plat'];
            $platStr = implode($plat, ',');
            $param['plat'] = $platStr;
        }
        //max id
        $id = Db::name('game_list')->max('id');
        $param['id'] = $id + 1;
        //添加游戏
        $result = Db::name('game_list')->insert($param);
        if (!empty($result)) {
            $this->success(lang('ADD_SUCCESS'), url("index"));
        } else {
            $this->error(lang('ADD_FAILED'));
        }
    }

    public function edit() {
        $id = $this->request->param('id');
        $data = Db::name('game_list')->where(['id' => $id])->find();
        if (!empty($data['plat'])) {
            $plats = explode(',', $data['plat']);
            $data['platStr'] = $this->getPlat($plats);
        } else {
            $data['platStr'] = $this->getPlat();
        }

        $this->assign('result', $data);
        return $this->fetch();
    }

    public function editPost() {
        $param = $this->request->param();
        $hasId = Db::name('game_list')->where(['game_id' => $param['game_id']])->find();
        if (!empty($hasId) && $hasId['id'] != $param['id']) {
            $this->error('此游戏ID已经存在，请重新输入');
        }
        $hasKey = Db::name('game_list')->where(['game_key' => $param['game_key']])->find();
        if (!empty($hasKey) && $hasKey['id'] != $param['id']) {
            $this->error('此游戏KEY已经存在，请重新输入');
        }
        if (!empty($param['plat'])) {
            $plat = $param['plat'];
            $platStr = implode($plat, ',');
            $param['plat'] = $platStr;
        }
        $result = Db::name('game_list')->update($param);
        if (!empty($result)) {
            $this->success(lang('EDIT_SUCCESS'), url("index", ['id' => $param['id']]));
        } else {
            $this->error(lang('EDIT_FAILED'));
        }
    }

    public function delete() {
        $id = $this->request->param('id', 0, 'intval');
        $where['id'] = $id;
        $cfg = Db::name('game_list')->where($where)->find();
        if (empty($cfg)) {
            $this->error(lang("无此游戏"));
        }
        //删除规则
        $gwhere['gameid'] = $cfg['game_id'];
        Db::name('gameid_config')->where($gwhere)->delete();
        //删除游戏
        $result = Db::name('game_list')->where($where)->delete();
        if ($result) {
            $this->success(lang("DELETE_SUCCESS"), url("index", ["id" => $cfg['id']]));
        } else {
            $this->error(lang("DELETE_FAILED"));
        }
    }

    public function editValide() {
        $id = $this->request->param('id', 0, 'intval');
        $data = Db::name('game_list')->where(['game_id' => $id])->field('game_id,game_name')->find();
        if (empty($data)) {
            $this->error('没有此游戏');
        }
        $configs = Db::name('gameid_config')->where(['gameid' => $id])->field('configid')->select()->toArray();
        $configids = array_column($configs, 'configid');
        $configData = Db::name('config_valide')->field('id,name_c')->select()->toArray();
        //生成校验选项
        $valideStr = '';
        foreach ($configData as $v) {
            if (in_array($v['id'], $configids)) {
                $valideStr .= ' <label class="checkbox-inline"><input type="checkbox" value="' . $v['id'] . '" checked  name="configid[]"/>' . $v['name_c'] . '</label>';
            } else {
                $valideStr .= ' <label class="checkbox-inline"><input type="checkbox" value="' . $v['id'] . '"  name="configid[]"/>' . $v['name_c'] . '</label>';
            }
        }
        $data['valide'] = $valideStr;
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function editPostValide() {
        $param = $this->request->param();
        if (empty($param['id'])) {
            $this->error('没有此游戏,无法编辑规则');
        }
        if (empty($param['configid'])) {
            $this->error('请选择规则后在保存');
        }
        $id = $param['id'];
        //检测已有的configid
        $where['gameid'] = $id;
        $configs = Db::name('gameid_config')->where($where)->field('configid')->select()->toArray();
        $configids = array_column($configs, 'configid');
        //合并已选择和已存储
        $post_config = $param['configid'];
        $all = array_merge($post_config, $configids);
        $allconfig = array_unique($all);
        //分离删除，添加
        $delData = [];
        foreach ($allconfig as $v) {
            if (in_array($v, $configids) && !in_array($v, $post_config)) {
                $delData[] = $v;
            } elseif (!in_array($v, $configids) && in_array($v, $post_config)) {
                $addData[] = array('gameid' => $id, 'configid' => $v);
            }
        }
        $flag = true;
        if (!empty($delData)) {
            $where['configid'] = ['in', $delData];
            $flag = Db::name('gameid_config')->where($where)->delete();
        }
        if ($flag && !empty($addData)) {
            $flag = Db::name('gameid_config')->insertAll($addData);
        }
        if (!empty($flag)) {
            $this->success(lang('EDIT_SUCCESS'), url("index", ['id' => $id]));
        } else {
            $this->error(lang('EDIT_FAILED'));
        }
    }

}
